package com.casework

class SearchService {
    static transactional = false

    def searchableService

    List searchForLegalCases(query) {
        def results = search(query)
        def legalCases = []

        for (result in results) {
            switch (result.class) {
                case (NaceCategory):
                    result = NaceCategory.get(result.id)
                    legalCases.addAll(getLegalCaseForIndustry(Nace.findAllByNaceCategory(result)))
                    break
                case (Nace):
                    legalCases.addAll(getLegalCaseForIndustry([result]))
                    break
                case (Firm):
                    legalCases.addAll(LegalCaseFirm.findAllByFirm(Firm.read(result.id))*.legalCase)
                    break
                case (Measure):
                    legalCases << Measure.read(result.id).legalCase
                    break
                case (LegalCase):
                    legalCases << LegalCase.read(result.id)
                    break
                default:
                    println "hit default"

            }
        }
        return legalCases.unique()
    }

    List searchForMeasuers(query) {
        def results = search(query)

        def measures = []

        for (result in results) {
            switch (result.class) {
                case (NaceCategory):
                    result = NaceCategory.get(result.id)
                    measures.addAll(getMeasuresForInForIndustry(Nace.findAllByNaceCategory(result)))
                    break
                case (Nace):
                    measures.addAll(getMeasuresForInForIndustry([result]))
                    break
                case (Firm):
                    measures.addAll(getMeasuresForFirm(result))
                    break
                case (Measure):
                    measures << Measure.read(result.id)
                    break
                case (LegalCase):
                    measures.addAll(result.measures)
                    break
                default:
                    println "hit default"

            }
        }

        return measures.unique()
    }

    private List getMeasuresForFirm(Firm firm) {
        def legalCases = LegalCaseFirm.findAllByFirm(Firm.read(firm.id))

        def measures = []
        legalCases.each{
            measures.addAll(it.legalCase.measures)
        }
        return measures
    }

    private List<Measure> getMeasuresForInForIndustry(List<Nace> industry) {
        def results = []
        for (ind in industry) {
            Nace nace = Nace.read(ind.id)
            def description = LegalCaseNaceFullDescription.findAllByDescription(nace)
            description.each {
                if (it) {
                    results.addAll it.legalCase.measures
                }
            }
        }
        return results
    }


    private List<LegalCase> getLegalCaseForIndustry(industry) {
        List results = []
        for (ind in industry) {
            Nace nace = Nace.read(ind.id)
            def description = LegalCaseNaceFullDescription.findAllByDescription(nace)
            description.each {
                if (it) {
                    results.addAll it.legalCase
                }
            }
        }
        return results
    }

    private List search(query) {
        def results = searchableService.searchEvery(query)

        if (!query.contains('"')) {
            query.split(' ').each {
                results.addAll(searchableService.searchEvery(it))
            }
        }

        return results.unique()
    }
}
